package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.GasAnalysisBo;
import org.dromara.system.domain.vo.GasAnalysisVo;
import org.dromara.system.domain.GasAnalysis;
import org.dromara.system.mapper.GasAnalysisMapper;
import org.dromara.system.service.IGasAnalysisService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 气体分析Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class GasAnalysisServiceImpl implements IGasAnalysisService {

    private final GasAnalysisMapper baseMapper;

    /**
     * 查询气体分析
     *
     * @param id 主键
     * @return 气体分析
     */
    @Override
    public GasAnalysisVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询气体分析列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 气体分析分页列表
     */
    @Override
    public TableDataInfo<GasAnalysisVo> queryPageList(GasAnalysisBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GasAnalysis> lqw = buildQueryWrapper(bo);
        Page<GasAnalysisVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的气体分析列表
     *
     * @param bo 查询条件
     * @return 气体分析列表
     */
    @Override
    public List<GasAnalysisVo> queryList(GasAnalysisBo bo) {
        LambdaQueryWrapper<GasAnalysis> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GasAnalysis> buildQueryWrapper(GasAnalysisBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GasAnalysis> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(GasAnalysis::getId);
        lqw.eq(bo.getZyid() != null, GasAnalysis::getZyid, bo.getZyid());
        lqw.eq(bo.getZylx() != null, GasAnalysis::getZylx, bo.getZylx());
        lqw.eq(bo.getQtlx() != null, GasAnalysis::getQtlx, bo.getQtlx());
        lqw.eq(StringUtils.isNotBlank(bo.getQtmc()), GasAnalysis::getQtmc, bo.getQtmc());
        lqw.eq(bo.getFxjg() != null, GasAnalysis::getFxjg, bo.getFxjg());
        lqw.eq(bo.getJgdw() != null, GasAnalysis::getJgdw, bo.getJgdw());
        lqw.eq(StringUtils.isNotBlank(bo.getFxr()), GasAnalysis::getFxr, bo.getFxr());
        lqw.eq(bo.getFxsj() != null, GasAnalysis::getFxsj, bo.getFxsj());
        lqw.eq(StringUtils.isNotBlank(bo.getCyfxbw()), GasAnalysis::getCyfxbw, bo.getCyfxbw());
        lqw.eq(StringUtils.isNotBlank(bo.getCash()), GasAnalysis::getCash, bo.getCash());
        lqw.eq(bo.getJcsfhg() != null, GasAnalysis::getJcsfhg, bo.getJcsfhg());
        lqw.eq(StringUtils.isNotBlank(bo.getQmtp()), GasAnalysis::getQmtp, bo.getQmtp());
        return lqw;
    }

    /**
     * 新增气体分析
     *
     * @param bo 气体分析
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(GasAnalysisBo bo) {
        GasAnalysis add = MapstructUtils.convert(bo, GasAnalysis.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改气体分析
     *
     * @param bo 气体分析
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(GasAnalysisBo bo) {
        GasAnalysis update = MapstructUtils.convert(bo, GasAnalysis.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GasAnalysis entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除气体分析信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
